package edu.nf.llmtest.common.sync;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
/**
 * @ Author bin
 * @ Create  2025/10/28 下午8:03
 **/
public class GenericTypeResolver {
    public static Class<?> resolveGenericType(Class<?> clazz, Class<?> targetInterface, int index) {
        for (Type type : clazz.getGenericInterfaces()) {
            if (type instanceof ParameterizedType) {
                ParameterizedType pt = (ParameterizedType) type;
                if (pt.getRawType().equals(targetInterface)) {
                    Type arg = pt.getActualTypeArguments()[index];
                    if (arg instanceof Class<?>) {
                        return (Class<?>) arg;
                    }
                }
            }
        }
        if (clazz.getSuperclass() != null) {
            return resolveGenericType(clazz.getSuperclass(), targetInterface, index);
        }
        throw new IllegalArgumentException("无法解析泛型类型: " + clazz);
    }
}
